Ext.namespace('Taxamatch');

Taxamatch.CSV = function(config) {
	var sources = new Ext.data.JsonStore({
			url: '../api/taxamatch.php'
		,	baseParams: { cmd: 'sources' }
		,	root: 'data'
		,	fields: ['id', 'active', 'name']//['name', 'value']
		,	autoLoad: true
	});
	
	Ext.apply(this, config, {
			fileUpload: true
		,	id: 'tm-csv'
		,	border: false
		,	title: 'Taxanomic CSV File Upload Form'
		,	autoHeight: true
		,	bodyStyle: 'padding: 10px 10px 0 10px;'
		,	labelWidth: 120
		,	defaults: {
//					anchor: '95%'
					msgTarget: 'side'
			}
		,	items: [{
					xtype: 'box'
				,	autoEl: {
						html: '<div style="padding-bottom:10px;">This tool is used to accept a CSV file where the first rows contains Darwin Core headers and analyzed the scientific names and returns a new CSV file that has results appended.  For more information please see the Help section.</div>'
					}
			},{
					store: sources
			//	,	itemId: 'source'
				,	xtype: 'combo'
				,	name: 'source'
				,	displayField: 'name'
				,	width: 200
				,	typeAhead: true
				,	mode: 'local'
				, 	allowBlank:false
				,	fieldLabel: 'Data Source'
				, 	forceSelection: true
				,	hiddenName: 'source'
				,	hiddenValue: 'id'
				//,	readOnly: true
				,	valueField: 'id'
				,	triggerAction: 'all'
				,	allowBlank: false
			}, {
					fieldLabel: "Url"
				,	name: 'url'
				,	xtype: 'textfield'
				,	width: 400
				,	vtype: 'url'
				,	autoScroll: true
			}, {
					xtype: 'fileuploadfield'
				,	id: 'form-file'
				,	emptyText: 'Select a file'
				,	fieldLabel: 'or CSV File'
				,	name: 'csv-file'
				,	width: 200
				,	buttonCfg: {
							text: ''
						,	iconCls: 'icon_upload'
					}
			}]
			,	buttonAlign: 'left'
			,	buttons: [{
						text: 'Upload'
					,	scope: this
					,	handler: this.uploadCsv
        },{
						text: 'Reset'
					,	scope: this
					,	handler: function(){
							this.getForm().reset();
						}
			}]
	});

	Taxamatch.CSV.superclass.constructor.apply(this, arguments);

};

Ext.extend( Taxamatch.CSV, Ext.FormPanel, {
	uploadCsv: function(){
		var form = this.getForm().getValues();
		if (this.getForm().isValid()){
			this.getForm().submit({
					url: '../api/taxamatch_csv.php'
				,	params: {
							cmd: 'download'
					}
			//	,	waitMsg: 'Uploading your CSV data file...'
				,	success: function(fp, o){
						console.log('o', o);
					}
				,	failure: function(fp, o){
						o.form.reset();
						Ext.Msg.alert('Failure',o.result.error);
					}
			});
		}
	} 
});